<?php
class CustomLoginForm extends MemberLoginForm {
    
    public function dologin($data) {
        if($this->performLogin($data)) {
              $this->redirectByGroup($data);
        } else {
            if($badLoginURL = Session::get("BadLoginURL")) {
                Director::redirect($badLoginURL);
            } else {
                Director::redirectBack();
            }
        }     
    }
    
    public function redirectByGroup($data)  {  
        
        $member = Member::currentUser();

        $Groups = DataObject::get("Group");
       
        foreach($Groups as $Group) {
        
            if($member && $member->inGroup($Group->ID) && $Group->GoToAdmin)  {  
                
                return Director::redirect(Director::baseURL() .'admin');
            }
            
            elseif($member && $member->inGroup($Group->ID)  && $Page = $Group->LinkedPage()) {  
                
                return Director::redirect($Page->Link());             
            }
            
            elseif($member && $member->inGroup($Group->ID)){
                return Director::redirect(Director::baseURL() .'home'); 
            }
        }
         
        return false;
    }
}